package com.yaozheng.vocationaltraining.service.impl;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.ICoursewarePaperView;
import com.yaozheng.vocationaltraining.service.CoursewarePaperService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * 课件试卷实现
 */
@EBean
public class CoursewarePaperServiceImpl implements CoursewarePaperService {
    ICoursewarePaperView iCoursewarePaperView;

    @Override
    public void init(ICoursewarePaperView iCoursewarePaperView) {
        this.iCoursewarePaperView = iCoursewarePaperView;
    }

    @Background
    @Override
    public void craetePaper(String coursewareId) {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/exam/main?type=2&cate=" + coursewareId,
                    iCoursewarePaperView.getToken(), new BaseView(iCoursewarePaperView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iCoursewarePaperView.createPaperSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iCoursewarePaperView.createPaperError(error);
                        }


                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iCoursewarePaperView.isResponseResult()) {
                iCoursewarePaperView
                        .createPaperError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
